package com.codeoss.framework.view.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Restrictions;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.metadata.TableData;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.IScriptService;
import com.codeoss.framework.expression.IScriptServiceStore;
import com.codeoss.framework.util.DictDef;
import com.codeoss.framework.util.LookupInfo;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.datasource.IDataSource;
import com.codeoss.framework.view.datasource.store.IDataSourceStore;
import com.codeoss.framework.view.model.SelectItem;
import com.codeoss.module.ir.model.IrDict;
import com.codeoss.module.ir.model.IrDictLine;
import com.codeoss.module.ir.service.IIrDictLineService;
import com.codeoss.module.ir.service.IIrDictService;

@Component
public class LookupUtil {
	@Autowired
	private IIrDictService irDictService;
	@Autowired
	private IIrDictLineService irDictLineService;
	@Autowired
	private IDataSourceStore dataSourceStore;
	@Autowired
	private IScriptServiceStore ScriptServiceStore;
	@Autowired
	private ITableStore tableStore;
	
	public List<SelectItem> getSelectItemList(YunContext yunContext, LookupInfo lookupInfo) {
		DictDef dictDef = lookupInfo.getDictDef();
		if (dictDef != null) {
			return getSelectItemListByDictDef(yunContext, dictDef);
		}
		DataSourceConfig dataSourceConfig = lookupInfo.getDataSourceConfig();
		if (dataSourceConfig != null) {
			return getSelectItemListByDataSource(yunContext, lookupInfo,null,null);
		}
		return null;
	}

	public List<SelectItem> getSelectItemList(YunContext yunContext, LookupInfo lookupInfo,Set<Object> keyValueSet) {
		return getSelectItemListByDataSource(yunContext, lookupInfo,keyValueSet,null);
	}
	
	public List<SelectItem> getSelectItemList(YunContext yunContext, LookupInfo lookupInfo,Object keyValue) {
		return getSelectItemListByDataSource(yunContext, lookupInfo,null,keyValue);
	}
	
	protected List<SelectItem> getSelectItemListByDictDef(YunContext yunContext, DictDef dictDef) {
		QueryWrapper<IrDict> irDictQueryWrapper = new QueryWrapper<IrDict>();
		irDictQueryWrapper.eq("code", dictDef.getRefCode());
		IrDict irDict = irDictService.getOne(irDictQueryWrapper);

		QueryWrapper<IrDictLine> irDictLineQueryWrapper = new QueryWrapper<IrDictLine>();
		irDictLineQueryWrapper.eq("ir_dict_id", irDict.getId());
		irDictLineQueryWrapper.eq("active", "1");
		List<IrDictLine> lineList = irDictLineService.list(irDictLineQueryWrapper);

		List<SelectItem> selectItemList = new ArrayList<SelectItem>();

		for (int i = 0; i < lineList.size(); i++) {
			IrDictLine irDictLine = lineList.get(i);
			SelectItem selectItem = new SelectItem(irDictLine.getCode(), irDictLine.getName());
			selectItemList.add(selectItem);
		}
		
		return selectItemList;
	}

	protected List<SelectItem> getSelectItemListByDataSource(YunContext yunContext, LookupInfo lookupInfo,Set<Object> keyValueSet,Object keyValue) {
		DataSourceConfig dataSourceConfig=lookupInfo.getDataSourceConfig();
		String sql="";
		Expression expression=dataSourceConfig.getWhereClause();
		if(expression!=null) {
			IScriptService scriptService=ScriptServiceStore.getScriptService(expression);
			String where=(String)scriptService.eval(expression.getScript(), yunContext.getVarValueMap());
			sql=where;
		}
		if(keyValueSet==null) {//不在列表视图下
			Expression dynamicWhereClauseExpression=lookupInfo.getDynamicWhereClauseExpression();
			if(dynamicWhereClauseExpression!=null) {
				IScriptService scriptService=ScriptServiceStore.getScriptService(dynamicWhereClauseExpression);
				String where=(String)scriptService.eval(dynamicWhereClauseExpression.getScript(), yunContext.getVarValueMap());
				if(!StringUtils.isEmpty(where)) {
					if(!StringUtils.isEmpty(sql)) {
						sql+=" and ("+where+")";
					}else {
						sql=where;
					}
				}
			}
		}
		Table table=tableStore.get(dataSourceConfig.getTableName());
		Query query=new Query();
		if(keyValueSet!=null) {
			query.add(Restrictions.in(table.getPrimaryKeyColumn().getColumnName(), (Collection)keyValueSet));
		}else if(keyValue!=null){
			query.add(Restrictions.eq(table.getPrimaryKeyColumn().getColumnName(), keyValue));
		}else {
			if(!StringUtils.isEmpty(sql)) {
				query.add(Restrictions.sql(sql));
			}
		}
		
		IDataSource dataSource=dataSourceStore.getDataSource(table.getAccessType());
		TableData tableData = dataSource.select(yunContext,table, query);
		List<SelectItem> selectItemList = new ArrayList<SelectItem>();

		for (int i = 0; i < tableData.getData().size(); i++) {
			Map<String, Object> dataMap = (Map<String, Object>) tableData.getData().get(i);
			SelectItem selectItem = new SelectItem(dataMap.get(lookupInfo.getValueColumn()),
					(String) dataMap.get(lookupInfo.getDisplayColumn()));
			selectItemList.add(selectItem);
		}
		return selectItemList;
	}
}
